package com.vincent.kdp.Activity;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.vincent.kdp.PostUrl;
import com.vincent.kdp.R;
import com.vincent.kdp.ui.adapter.BumenAdapter;
import com.vincent.kdp.ui.adapter.ShangjiaAdapter;
import com.vincent.kdp.ui.work.ShangjiaInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BumenActivity extends Activity {

    private ListView bumen;
    private RequestQueue requestQueue;
    private BumenAdapter adapter;
    private int id2;

    public static BumenActivity instance = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        instance=this;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bumen);
        Bundle bundle = getIntent().getExtras();
        id2= Integer.parseInt(bundle.getString("id"));
        intView();
        initData();
    }

    private void initData() {
        requestQueue= Volley.newRequestQueue(this);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, PostUrl.BUMEN, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(s);
                    JSONArray array = (JSONArray) jsonObject.get("data");
                    List<ShangjiaInfo> list = new ArrayList<ShangjiaInfo>();
                    for (int i = 0; i < array.length(); i++) {
                        ShangjiaInfo info=new ShangjiaInfo();
                        JSONObject temp = (JSONObject) array.get(i);
                        String firstDepartId = "";
                        String name = "";
                        if (temp.has("id")) {
                            firstDepartId = temp.getString("id");
                        }
                        if (temp.has("name")) {
                            name = temp.getString("name");
                        }
                        info.setId(Integer.parseInt(firstDepartId));
                        info.setXinxi(name);
                        list.add(info);
                    }
                    adapter.addItems(list);
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("companyId", String.valueOf(id2));
                return map;
            }


        };
        requestQueue.add(stringRequest);
    }

    private void intView() {
        bumen=(ListView)findViewById(R.id.bumen);
        adapter=new BumenAdapter(this);
        bumen.setAdapter(adapter);
    }
}
